define(function (require, exports, module) {
    var angular = require('angular');
    require("angular-ui");
    
    require('/static_public/js/angular_directive/location');
    
    var util = require('util');
    require("angular-interceptor");
    
    angular.module('app', ['ngUI','ngInterceptor','locationSelector'])
        .constant('api.path.activity', "/api/index.php?r=xmf/xmf-activity/")
        .factory('ActivityService', [
            '$http',
            'api.path.activity',
            function ($http, api_path) {
                var httpClient = {
                    get: function (api, data) {
                        var url = api_path + api;
                        var search = (data && data.search) || {};
                        angular.forEach(search, function (val, key) {
                            url += "&" + key + "=" + val;
                        });

                        return $http.get(url)
                            .then(function (res) {
                                if (res.retCode === "0") {
                                    return res;
                                }
                            })
                    },
                    post: function (api, data) {
                        return $http.post(api_path + api, (data && data.data) || {})
                            .then(function (res) {
                                res = res.data;
                                if (res.retCode === "0") {
                                    return res;
                                }
                            })
                    }
                }
                return {
                    getActivityInfo: function (data) {
                        return httpClient.post('activity-detail', data)
                    },
                    getTemplateUrl: function () {
                        return api_path + "download-template";
                    },
                    getResultUrl: function(){
                        return api_path + "export-talk-result";
                    }
                }
            }
        ])
        .controller('MainController', [
            '$scope',
            '$rootScope',
            '$window',
            'ActivityService',
            function ($scope, $rootScope, $window,service) {
                
                $scope.customerImporter = {
                    onDialogShow: function ($dialog, scope) {
                        scope.proj_id = $scope.proj_id;
                        scope.exportTemplate = function () {
                            util.download(service.getTemplateUrl(),{
                                act_id: $scope.activityId
                            });
                        };
                        scope.onUploadStatusChanged = function (statusData) {
                            switch (statusData.status) {
                                case 'error':
                                    $rootScope.$emit('error', "上传失败");
                                    break;
                                case 'success':
                                    var data = statusData.response && statusData.response.data;
                                    if (data && data.fail_count) {
                                        var msg = "";
                                        for (var i = 0; i < data.error.length; i++) {
                                            var error = data.error[i];
                                            msg += '第' + error.row_id + '行创建出错，原因： ' + error.msg + '<br>';
                                        }

                                        $rootScope.$emit('error', msg, true);
                                        return;
                                    }
                                    $rootScope.$emit('success', "上传成功");
                                    $dialog.close();
                                    break;
                            }
                        };
                    },
                    save: function ($dialog, scope) {
                        scope.uploaderModel.upload();
                        return false;
                    }
                };
                
                $scope.proj_id = util.getUrlPara('pid');
                
                $scope.exportExpandResult = function(){
                    util.download(service.getResultUrl(),{
                        proj_id: $scope.proj_id,
                        act_id: $scope.activityId
                    });
                };
                
                $scope.getActivityInfo = function(id){
                    service.getActivityInfo({
                        data: {
                            act_id: id,
                            proj_id: $scope.proj_id
                        }
                    })
                        .then(function(res){
                            $scope.activity = res.data;
                                        
                            $scope.detail = $scope.activity.act_detail;
                            $scope.activity_type_id = $scope.activity.act_detail.act_type_id;
                            $scope.activity_token = $scope.activity.act_detail.mtoken;
                        })
                };   
               
                $scope.activityId = util.getUrlPara("id");
                
				$scope.getActivityInfo($scope.activityId);
                
                $scope.cancel = function(){
                    $window.location.href = "/page/intermediary_organ/bee/index.shtml?token=" + util.getUrlPara('token') + "#/activity"
                };
                
                $scope.edit = function(){
                    $window.location.href = "/page/intermediary_organ/bee/activity/edit.html?token=" + util.getUrlPara('token') + "&id=" + $scope.activityId + "&pid=" + $scope.proj_id;
                };
                
                $scope.viewCustomerConfirm = function(){
                    $window.location.href = "/page/ditch/index.shtml?code=guestAllot&token=" + util.getUrlPara('token') + '&proj_id=' + $scope.proj_id;
                };
            }
        ])
    
    angular.bootstrap(document, ['app']);
});
